<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <!-- 新增的一级页面权限 type 1  pid '0' -->
          <el-button type="primary" size="small" @click="clickAdd(1, '0')">添加权限</el-button>
        </div>
        <!--
          如果需要el-table展示树形数据
          1. 有一个树形数据(children)
          2. 必须执行row-key="id"
         -->
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <!-- 新增的是 二级按钮权限  type 2   pid 父级的id -->
              <el-button v-if="row.type === 1" size="small" type="text" @click="clickAdd(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="clickEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="clickDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 新增/编辑 的弹窗 -->
      <!-- 新增/编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="title" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- switch -->
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button size="small" @click="closeDialog">取消</el-button>
          <el-button size="small" type="primary" @click="submit">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用 (闹着玩)
        name: '', // 权限名称
        code: '', // 权限标识,用于将来判断
        description: '', // 权限描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      // console.log(res)
      // 这里一级权限的pid是 '0'
      this.list = transListToTreeData(data, '0')
      // this.list = data
    },
    clickAdd(type, pid) {
      this.showDialog = true

      this.formData.type = type
      this.formData.pid = pid
    },
    closeDialog() {
      this.showDialog = false
      // 没有表单校验 无法通过resetFields重置表单
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用 (闹着玩)
        name: '', // 权限名称
        code: '', // 权限标识,用于将来判断
        description: '', // 权限描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    async submit() {
      // 不用表单校验
      if (this.formData.id) {
        await reqUpdatePermission(this.formData)
        this.$message.success('编辑权限成功')
      } else {
        await reqAddPermission(this.formData)
        this.$message.success('新增权限成功')
      }

      this.closeDialog()
      this.getPermissionList()
    },
    clickDel(id) {
      this.$confirm('确认删除么?', '温馨提示').then(async() => {
        const res = await reqDelPermission(id)
        console.log(res)
        this.$message.success('删除权限成功')
        this.getPermissionList()
      }).catch(() => {
        console.log('取消')
      })
    },
    async clickEdit(id) {
      this.showDialog = true
      const { data } = await reqGetPermissionDetail(id)
      console.log(data)
      this.formData = data
    }
  }
}
</script>

<style>

</style>
